# -*- coding: utf-8 -*-
# 正则表达式

import re

print(re.match(r'^\d{3}\-\d{3,8}$','010-123456'))
print(re.match(r'^\d{3}\-\d{3,8}$','010 123456'))

test = '010-123456'
if re.match(r'^\d{3}\-\d{3,8}$',test):
	print('ok')
else:
	print('failed')

# 切分字符串
print('a b    c'.split(' '))

print(re.split(r'\s+','a b    c'))
print(re.split(r'[\s\,\;]+','a, b,;;;    c'))


# 分组 用()表示要提取的分组（子串）

m = re.match(r'^(\d{3})\-(\d{3,8})$','010-123456')
print(m.group(0))
print(m.group(1))
print(m.group(2))

t = '19:05:30'
m = re.match(r'^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])$', t)
print(m.groups())

# 最后需要特别指出的是，正则匹配默认是贪婪匹配，也就是匹配尽可能多的字符
# 贪婪匹配
print(re.match(r'^(\d+)(0*)$', '102300').groups())
# 非贪婪匹配
print(re.match(r'^(\d+?)(0*)$', '102300').groups())


# 编译 提高效率，省略编译步骤
re_telephone = re.compile(r'^(\d{3})\-(\d{3,8})$')
# 使用
print(re_telephone.match('010-123456').groups())